package com.appclub.kitsilano.calendar;

import java.util.Calendar;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.appclub.kitsilano.MainActivity;
import com.appclub.kitsilano.R;
import com.viewpagerindicator.TabPageIndicator;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

public class Calendar_ extends SherlockFragmentActivity implements
		ActionBar.TabListener {
	private String[] months;
	private Resources res;
	private FragmentPagerAdapter adapter;
	private ViewPager pager;
	private TabPageIndicator indicator;
	String[] ITEMS;
	ListAdapter lvadapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		setTheme(R.style.Sherlock___Theme_Light);
		super.onCreate(savedInstanceState);
		int[] IPP = getResources().getIntArray(R.array.IPP);
		int[] Day0 = getResources().getIntArray(R.array.Day0);
		int[] Pro_D = getResources().getIntArray(R.array.Pro_D);

		Calendar c = Calendar.getInstance();
		int Day = c.get(Calendar.DAY_OF_YEAR);
		ITEMS = new String[3];
		for (int i = 0; i < IPP.length; i++) {
			if (IPP[i] - Day < 0 && i == IPP.length - 1) {
				ITEMS[0] = String.valueOf(Day - IPP[i - 1]);
			}
		}
		for (int i = 0; i < Day0.length; i++) {
			if (Day0[i] - Day < 0 && i == Day0.length - 1) {
				ITEMS[1] = String.valueOf(Day - Day0[i - 1]);
			}
		}
		for (int i = 0; i < Pro_D.length; i++) {
			if (Pro_D[i] - Day < 0 && i == Pro_D.length - 1) {
				ITEMS[2] = String.valueOf(Day - Pro_D[i - 1]);
			}
		}
		lvadapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, ITEMS);

		res = getResources();
		months = res.getStringArray(R.array.months);

		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		ActionBar.Tab tab1 = getSupportActionBar().newTab();
		ActionBar.Tab tab2 = getSupportActionBar().newTab();
		tab1.setText("calendar");
		tab2.setText("holidays");
		tab1.setTabListener(this);
		tab2.setTabListener(this);
		getSupportActionBar().addTab(tab1);
		getSupportActionBar().addTab(tab2);
		tab1.setTabListener(tabListener);
		tab2.setTabListener(tabListener2);

		setContentView(R.layout.calendar_calendar_tab);

		int month = Calendar.getInstance().get(Calendar.MONTH);

		adapter = new Months(getSupportFragmentManager());

		pager = (ViewPager) findViewById(R.id.months_pages);
		pager.setAdapter(adapter);
		pager.setCurrentItem(month);

		indicator = (TabPageIndicator) findViewById(R.id.months);
		indicator.setViewPager(pager);
		indicator.setCurrentItem(month);

	}

	ActionBar.TabListener tabListener = new ActionBar.TabListener() {
		public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
			setContentView(R.layout.calendar_calendar_tab);
			res = getResources();
			months = res.getStringArray(R.array.months);
			adapter = new Months(getSupportFragmentManager());

			pager = (ViewPager) findViewById(R.id.months_pages);
			pager.setAdapter(adapter);

			indicator = (TabPageIndicator) findViewById(R.id.months);
			indicator.setViewPager(pager);

		}

		public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
			pager.removeAllViews();
			pager.setAdapter(null);
			indicator.removeAllViews();
		}

		public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
		}
	};
	ActionBar.TabListener tabListener2 = new ActionBar.TabListener() {
		public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
			setContentView(R.layout.calendar_holidays_tab);
			// ListView lv = (ListView) findViewById(R.id.listView1);
			// lv.setAdapter(lvadapter);
		}

		public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
		}

		public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
		}
	};

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			Intent intent = new Intent(this, MainActivity.class);
			startActivity(intent);
			overridePendingTransition(R.anim.right_in, R.anim.right_out);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void onTabSelected(Tab tab, FragmentTransaction ft) {
	}

	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
	}

	public void onTabReselected(Tab tab, FragmentTransaction ft) {
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Intent intent = new Intent(this, MainActivity.class);
			startActivity(intent);
			overridePendingTransition(R.anim.right_in, R.anim.right_out);
			this.finish();
		}
		return true;
	}

	class Months extends FragmentPagerAdapter {
		public Months(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			return CalendarFragment
					.newInstance(months[position % months.length]);
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return months[position % months.length].toUpperCase();
		}

		@Override
		public int getCount() {
			return months.length;
		}
	}
}